package com.pz.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;

import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;


@Configuration
//去除掉其他的跨域配置，如 renren-fast 之前的跨域配置
public class CORSConfiguration {

    //@Bean
    public CorsWebFilter corsWebFilter(){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

        CorsConfiguration corsConfiguration = new CorsConfiguration();

        //1、配置跨域
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        // springboot升级成2.4.0以上时对AllowedOrigin设置发生了改变，不能有”*“,可以替换成AllowedOriginPattern
        corsConfiguration.addAllowedOriginPattern("*"); //允许所有请求来源
        corsConfiguration.setAllowCredentials(true); //允许 携带 cookie

        source.registerCorsConfiguration("/**", corsConfiguration); //所有路径
        return new CorsWebFilter(source);
    }
}
